#include "apue.h"
#include "error.c"
#include <fcntl.h>

int main(int argc, char const *argv[])
{
    if(argc != 3)
        err_quit("param error");
    
    int r_fd, w_fd;
    if((r_fd = open(argv[1], O_RDONLY)) < 0){
        err_quit("open file %s error", argv[1]);
    }

    if((w_fd = open(argv[2], O_WRONLY | O_CREAT)) < 0){
        err_quit("open | creat file %s error", argv[2]);
    }

    int val;
    if((val = fcntl(w_fd, F_GETFL, 0)) < 0){
        err_sys("get write file status error");
    }else{
        val |= O_APPEND;
        if(fcntl(w_fd, F_SETFL, val) < 0){
            err_sys("set write fd append status error");
        }
    }


    char buf[4096];
    ssize_t nsize;
    while((nsize = read(r_fd, buf, 4096))){
        if(write(w_fd, buf, nsize) == -1){
            err_quit("write data error");
        }
    }

    close(r_fd);
    close(w_fd);
    return 0;
}
